package com.itheima.threadtest6_2;


import java.util.ArrayList;
import java.util.Collections;

public class MyThread extends Thread{

    //创建临时变量存储个抽奖箱的内容
    ArrayList<Integer> list ;
//    static ArrayList<Integer> list1 = new ArrayList<>() ;
//    static ArrayList<Integer> list2 = new ArrayList<>() ;

    public MyThread(ArrayList<Integer> list) {
        this.list = list;
    }

    public void run() {
        ArrayList<Integer> boxList = new ArrayList<>() ;    // 每个线程会创建自己的集合
        while(true) {
            synchronized (MyThread.class) {
                if (list.isEmpty()) {
                    // 抽奖结束
                    System.out.println(this.getName() + "：" + boxList);
                    break ;
                } else {
                    Collections.shuffle(list);
                    int prize = list.remove(0) ;
                    boxList.add(prize) ;
                }
            }

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
